package com.jike.noobmoney.util;

import android.util.Base64;

import com.toomee.mengplus.common.utils.CloseUtils;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by kekex on 2019/4/16.
 * 加密
 */

public class EncryptUtil {
    public static String DES_Transformation = "DES/ECB/NoPadding";
    private static final String DES_Algorithm = "DES";
    public static String TripleDES_Transformation = "DESede/ECB/NoPadding";
    private static final String TripleDES_Algorithm = "DESede";
    public static String AES_Transformation = "AES/ECB/NoPadding";
    private static final String AES_Algorithm = "AES";
    private static final char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    private EncryptUtil() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static String encryptMD2ToString(String var0) {
        return encryptMD2ToString(var0.getBytes());
    }

    public static String encryptMD2ToString(byte[] var0) {
        return bytes2HexString(encryptMD2(var0));
    }

    public static byte[] encryptMD2(byte[] var0) {
        return hashTemplate(var0, "MD2");
    }

    public static String encryptMD5ToString(String var0) {
        return encryptMD5ToString(var0.getBytes());
    }

    public static String encryptMD5ToString(String var0, String var1) {
        return bytes2HexString(encryptMD5((var0 + var1).getBytes()));
    }

    public static String encryptMD5ToString(byte[] var0) {
        return bytes2HexString(encryptMD5(var0));
    }

    public static String encryptMD5ToString(byte[] var0, byte[] var1) {
        if(var0 != null && var1 != null) {
            byte[] var2 = new byte[var0.length + var1.length];
            System.arraycopy(var0, 0, var2, 0, var0.length);
            System.arraycopy(var1, 0, var2, var0.length, var1.length);
            return bytes2HexString(encryptMD5(var2));
        } else {
            return null;
        }
    }

    public static byte[] encryptMD5(byte[] var0) {
        return hashTemplate(var0, "MD5");
    }

    public static String encryptMD5File2String(String var0) {
        File var1 = isSpace(var0)?null:new File(var0);
        return encryptMD5File2String(var1);
    }

    public static byte[] encryptMD5File(String var0) {
        File var1 = isSpace(var0)?null:new File(var0);
        return encryptMD5File(var1);
    }

    public static String encryptMD5File2String(File var0) {
        return bytes2HexString(encryptMD5File(var0));
    }

    public static byte[] encryptMD5File(File var0) {
        if(var0 == null) {
            return null;
        } else {
            FileInputStream var1 = null;

            Object var4;
            try {
                var1 = new FileInputStream(var0);
                MessageDigest var3 = MessageDigest.getInstance("MD5");
                DigestInputStream var2 = new DigestInputStream(var1, var3);
                byte[] var11 = new byte[262144];

                while(var2.read(var11) > 0) {
                    ;
                }

                var3 = var2.getMessageDigest();
                byte[] var5 = var3.digest();
                return var5;
            } catch (IOException | NoSuchAlgorithmException var9) {
                var9.printStackTrace();
                var4 = null;
            } finally {
                CloseUtils.closeIO(new Closeable[]{var1});
            }

            return (byte[])var4;
        }
    }

    public static String encryptSHA1ToString(String var0) {
        return encryptSHA1ToString(var0.getBytes());
    }

    public static String encryptSHA1ToString(byte[] var0) {
        return bytes2HexString(encryptSHA1(var0));
    }

    public static byte[] encryptSHA1(byte[] var0) {
        return hashTemplate(var0, "SHA1");
    }

    public static String encryptSHA224ToString(String var0) {
        return encryptSHA224ToString(var0.getBytes());
    }

    public static String encryptSHA224ToString(byte[] var0) {
        return bytes2HexString(encryptSHA224(var0));
    }

    public static byte[] encryptSHA224(byte[] var0) {
        return hashTemplate(var0, "SHA224");
    }

    public static String encryptSHA256ToString(String var0) {
        return encryptSHA256ToString(var0.getBytes());
    }

    public static String encryptSHA256ToString(byte[] var0) {
        return bytes2HexString(encryptSHA256(var0));
    }

    public static byte[] encryptSHA256(byte[] var0) {
        return hashTemplate(var0, "SHA256");
    }

    public static String encryptSHA384ToString(String var0) {
        return encryptSHA384ToString(var0.getBytes());
    }

    public static String encryptSHA384ToString(byte[] var0) {
        return bytes2HexString(encryptSHA384(var0));
    }

    public static byte[] encryptSHA384(byte[] var0) {
        return hashTemplate(var0, "SHA384");
    }

    public static String encryptSHA512ToString(String var0) {
        return encryptSHA512ToString(var0.getBytes());
    }

    public static String encryptSHA512ToString(byte[] var0) {
        return bytes2HexString(encryptSHA512(var0));
    }

    public static byte[] encryptSHA512(byte[] var0) {
        return hashTemplate(var0, "SHA512");
    }

    private static byte[] hashTemplate(byte[] var0, String var1) {
        if(var0 != null && var0.length > 0) {
            try {
                MessageDigest var2 = MessageDigest.getInstance(var1);
                var2.update(var0);
                return var2.digest();
            } catch (NoSuchAlgorithmException var3) {
                var3.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static String encryptHmacMD5ToString(String var0, String var1) {
        return encryptHmacMD5ToString(var0.getBytes(), var1.getBytes());
    }

    public static String encryptHmacMD5ToString(byte[] var0, byte[] var1) {
        return bytes2HexString(encryptHmacMD5(var0, var1));
    }

    public static byte[] encryptHmacMD5(byte[] var0, byte[] var1) {
        return hmacTemplate(var0, var1, "HmacMD5");
    }

    public static String encryptHmacSHA1ToString(String var0, String var1) {
        return encryptHmacSHA1ToString(var0.getBytes(), var1.getBytes());
    }

    public static String encryptHmacSHA1ToString(byte[] var0, byte[] var1) {
        return bytes2HexString(encryptHmacSHA1(var0, var1));
    }

    public static byte[] encryptHmacSHA1(byte[] var0, byte[] var1) {
        return hmacTemplate(var0, var1, "HmacSHA1");
    }

    public static String encryptHmacSHA224ToString(String var0, String var1) {
        return encryptHmacSHA224ToString(var0.getBytes(), var1.getBytes());
    }

    public static String encryptHmacSHA224ToString(byte[] var0, byte[] var1) {
        return bytes2HexString(encryptHmacSHA224(var0, var1));
    }

    public static byte[] encryptHmacSHA224(byte[] var0, byte[] var1) {
        return hmacTemplate(var0, var1, "HmacSHA224");
    }

    public static String encryptHmacSHA256ToString(String var0, String var1) {
        return encryptHmacSHA256ToString(var0.getBytes(), var1.getBytes());
    }

    public static String encryptHmacSHA256ToString(byte[] var0, byte[] var1) {
        return bytes2HexString(encryptHmacSHA256(var0, var1));
    }

    public static byte[] encryptHmacSHA256(byte[] var0, byte[] var1) {
        return hmacTemplate(var0, var1, "HmacSHA256");
    }

    public static String encryptHmacSHA384ToString(String var0, String var1) {
        return encryptHmacSHA384ToString(var0.getBytes(), var1.getBytes());
    }

    public static String encryptHmacSHA384ToString(byte[] var0, byte[] var1) {
        return bytes2HexString(encryptHmacSHA384(var0, var1));
    }

    public static byte[] encryptHmacSHA384(byte[] var0, byte[] var1) {
        return hmacTemplate(var0, var1, "HmacSHA384");
    }

    public static String encryptHmacSHA512ToString(String var0, String var1) {
        return encryptHmacSHA512ToString(var0.getBytes(), var1.getBytes());
    }

    public static String encryptHmacSHA512ToString(byte[] var0, byte[] var1) {
        return bytes2HexString(encryptHmacSHA512(var0, var1));
    }

    public static byte[] encryptHmacSHA512(byte[] var0, byte[] var1) {
        return hmacTemplate(var0, var1, "HmacSHA512");
    }

    private static byte[] hmacTemplate(byte[] var0, byte[] var1, String var2) {
        if(var0 != null && var0.length != 0 && var1 != null && var1.length != 0) {
            try {
                SecretKeySpec var3 = new SecretKeySpec(var1, var2);
                Mac var4 = Mac.getInstance(var2);
                var4.init(var3);
                return var4.doFinal(var0);
            } catch (NoSuchAlgorithmException | InvalidKeyException var5) {
                var5.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static byte[] encryptDES2Base64(byte[] var0, byte[] var1) {
        return base64Encode(encryptDES(var0, var1));
    }

    public static String encryptDES2HexString(byte[] var0, byte[] var1) {
        return bytes2HexString(encryptDES(var0, var1));
    }

    public static byte[] encryptDES(byte[] var0, byte[] var1) {
        return desTemplate(var0, var1, "DES", DES_Transformation, true);
    }

    public static byte[] decryptBase64DES(byte[] var0, byte[] var1) {
        return decryptDES(base64Decode(var0), var1);
    }

    public static byte[] decryptHexStringDES(String var0, byte[] var1) {
        return decryptDES(hexString2Bytes(var0), var1);
    }

    public static byte[] decryptDES(byte[] var0, byte[] var1) {
        return desTemplate(var0, var1, "DES", DES_Transformation, false);
    }

    public static byte[] encrypt3DES2Base64(byte[] var0, byte[] var1) {
        return base64Encode(encrypt3DES(var0, var1));
    }

    public static String encrypt3DES2HexString(byte[] var0, byte[] var1) {
        return bytes2HexString(encrypt3DES(var0, var1));
    }

    public static byte[] encrypt3DES(byte[] var0, byte[] var1) {
        return desTemplate(var0, var1, "DESede", TripleDES_Transformation, true);
    }

    public static byte[] decryptBase64_3DES(byte[] var0, byte[] var1) {
        return decrypt3DES(base64Decode(var0), var1);
    }

    public static byte[] decryptHexString3DES(String var0, byte[] var1) {
        return decrypt3DES(hexString2Bytes(var0), var1);
    }

    public static byte[] decrypt3DES(byte[] var0, byte[] var1) {
        return desTemplate(var0, var1, "DESede", TripleDES_Transformation, false);
    }

    public static byte[] encryptAES2Base64(byte[] var0, byte[] var1) {
        return base64Encode(encryptAES(var0, var1));
    }

    public static String encryptAES2HexString(byte[] var0, byte[] var1) {
        return bytes2HexString(encryptAES(var0, var1));
    }

    public static byte[] encryptAES(byte[] var0, byte[] var1) {
        return desTemplate(var0, var1, "AES", AES_Transformation, true);
    }

    public static byte[] decryptBase64AES(byte[] var0, byte[] var1) {
        return decryptAES(base64Decode(var0), var1);
    }

    public static byte[] decryptHexStringAES(String var0, byte[] var1) {
        return decryptAES(hexString2Bytes(var0), var1);
    }

    public static byte[] decryptAES(byte[] var0, byte[] var1) {
        return desTemplate(var0, var1, "AES", AES_Transformation, false);
    }

    public static byte[] desTemplate(byte[] var0, byte[] var1, String var2, String var3, boolean var4) {
        if(var0 != null && var0.length != 0 && var1 != null && var1.length != 0) {
            try {
                SecretKeySpec var5 = new SecretKeySpec(var1, var2);
                Cipher var6 = Cipher.getInstance(var3);
                SecureRandom var7 = new SecureRandom();
                var6.init(var4?1:2, var5, var7);
                return var6.doFinal(var0);
            } catch (Throwable var8) {
                var8.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    private static String bytes2HexString(byte[] var0) {
        if(var0 == null) {
            return null;
        } else {
            int var1 = var0.length;
            if(var1 <= 0) {
                return null;
            } else {
                char[] var2 = new char[var1 << 1];
                int var3 = 0;

                for(int var4 = 0; var3 < var1; ++var3) {
                    var2[var4++] = hexDigits[var0[var3] >>> 4 & 15];
                    var2[var4++] = hexDigits[var0[var3] & 15];
                }

                return new String(var2);
            }
        }
    }

    private static byte[] hexString2Bytes(String var0) {
        if(isSpace(var0)) {
            return null;
        } else {
            int var1 = var0.length();
            if(var1 % 2 != 0) {
                var0 = "0" + var0;
                ++var1;
            }

            char[] var2 = var0.toUpperCase().toCharArray();
            byte[] var3 = new byte[var1 >> 1];

            for(int var4 = 0; var4 < var1; var4 += 2) {
                var3[var4 >> 1] = (byte)(hex2Dec(var2[var4]) << 4 | hex2Dec(var2[var4 + 1]));
            }

            return var3;
        }
    }

    private static int hex2Dec(char var0) {
        if(var0 >= 48 && var0 <= 57) {
            return var0 - 48;
        } else if(var0 >= 65 && var0 <= 70) {
            return var0 - 65 + 10;
        } else {
            throw new IllegalArgumentException();
        }
    }

    private static byte[] base64Encode(byte[] var0) {
        return Base64.encode(var0, 2);
    }

    private static byte[] base64Decode(byte[] var0) {
        return Base64.decode(var0, 2);
    }

    private static boolean isSpace(String var0) {
        if(var0 == null) {
            return true;
        } else {
            int var1 = 0;

            for(int var2 = var0.length(); var1 < var2; ++var1) {
                if(!Character.isWhitespace(var0.charAt(var1))) {
                    return false;
                }
            }

            return true;
        }
    }
}
